{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "CRL_12 winners.ipynb",
      "provenance": [],
      "collapsed_sections": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "WptFJCiq72yh",
        "outputId": "3e25aaae-9d57-4f78-b95f-8467ddc4e94e"
      },
      "source": [
        "import numpy as np\r\n",
        "import random\r\n",
        "import pandas as pd\r\n",
        "\r\n",
        "def simulation(n_players, max_wins, n_lives):\r\n",
        "  class player:\r\n",
        "    lives = n_lives\r\n",
        "    consecutive_wins = 0\r\n",
        "\r\n",
        "    def __init__(self):\r\n",
        "      pass\r\n",
        "\r\n",
        "  def pick_alive():\r\n",
        "    return pool_alive.pop(random.randint(0, len(pool_alive)-1))\r\n",
        "\r\n",
        "  def fight(max_wins):\r\n",
        "    try:\r\n",
        "      a = pick_alive()\r\n",
        "      b = pick_alive()\r\n",
        "\r\n",
        "      #result\r\n",
        "      result = random.randint(0, 1)\r\n",
        "      \r\n",
        "      if result == 0:\r\n",
        "        a.lives -= 1\r\n",
        "        b.consecutive_wins += 1\r\n",
        "      else:\r\n",
        "        b.lives -= 1\r\n",
        "        a.consecutive_wins += 1\r\n",
        "      \r\n",
        "      #print(result, 'a:', a.lives, 'b:', b.lives)\r\n",
        "      #die or live\r\n",
        "      if a.consecutive_wins == max_wins:\r\n",
        "        pool_done.append(a)\r\n",
        "      else:\r\n",
        "        if a.lives == 0:\r\n",
        "          pool_done.append(a)\r\n",
        "        else:\r\n",
        "          pool_alive.append(a)\r\n",
        "\r\n",
        "      if b.consecutive_wins == max_wins:\r\n",
        "        pool_done.append(b)\r\n",
        "      else:\r\n",
        "        if b.lives == 0:\r\n",
        "          pool_done.append(b)\r\n",
        "        else:\r\n",
        "          pool_alive.append(b)\r\n",
        "\r\n",
        "      #print('tried', pool_alive, a.lives, b.lives, pool_done)\r\n",
        "    except:\r\n",
        "      #print(pool_alive, 'k')\r\n",
        "      #last player remaining\r\n",
        "      pool_done.append(a)\r\n",
        "      return 0\r\n",
        "\r\n",
        "  #print(len(pool_alive))\r\n",
        "\r\n",
        "  pool_alive = [player() for x in range(n_players)]\r\n",
        "  pool_done = list()\r\n",
        "\r\n",
        "  #fight until there is no one left\r\n",
        "  #last player gets moved into the other bracket\r\n",
        "  while len(pool_alive) > 0:\r\n",
        "    if fight(max_wins) == 0:\r\n",
        "      break\r\n",
        "\r\n",
        "  players_done = list()\r\n",
        "  for d in pool_done:\r\n",
        "    players_done.append([d.lives, d.consecutive_wins])\r\n",
        "\r\n",
        "  players_alive = list()\r\n",
        "  for k in pool_alive:\r\n",
        "    players_alive.append([k.lives, k.consecutive_wins])\r\n",
        "\r\n",
        "  total = pd.DataFrame(players_done+players_alive)\r\n",
        "  total\r\n",
        "\r\n",
        "  return total, total[1][total[1]==12].count()/n_players\r\n",
        "\r\n",
        "_, winners = simulation(1000000, 12, 3)\r\n",
        "winners"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "0.006397"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 173
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "n4xB-RFfOWIB",
        "outputId": "a1fdd5b8-e422-4bfe-ec4f-bc6ae02d93e8"
      },
      "source": [
        "import numpy as np\r\n",
        "\r\n",
        "a = [[['a', 'l']], [['a', 'h']]]\r\n",
        "\r\n",
        "def prepare_dict(list1):\r\n",
        "  list1 = np.array(list1).flatten()\r\n",
        "\r\n",
        "  #create set\r\n",
        "  set1 = set()\r\n",
        "  [set1.add(el) for el in list1]\r\n",
        "\r\n",
        "  #create dict\r\n",
        "  label_2_int = {list(set1)[i]:i for i in range(len(list(set1)))}\r\n",
        "  return label_2_int\r\n",
        "\r\n",
        "def index_based_encoder(list1, label_2_int):\r\n",
        "  list1 = np.array(list1).flatten()\r\n",
        "  #index-based encoding\r\n",
        "  y_index = [label_2_int[el] for el in list1]\r\n",
        "  return y_index\r\n",
        "\r\n",
        "def one_hot_encoder(list1, label_2_int):\r\n",
        "  list1 = np.array(list1).flatten()\r\n",
        "\r\n",
        "  #one_hot encoding\r\n",
        "  def fill_array(array, index):\r\n",
        "    array[index] = 1\r\n",
        "    return array\r\n",
        "\r\n",
        "  #index-based encoding\r\n",
        "  y_one_hot = [fill_array(np.zeros(len(label_2_int)), el) for el in list1]\r\n",
        "  return y_one_hot\r\n",
        "\r\n",
        "label_2_int = prepare_dict(a)\r\n",
        "y_index = index_based_encoder(a, label_2_int)\r\n",
        "y_one_hot = one_hot_encoder(y_index, label_2_int)\r\n",
        "\r\n",
        "print('y:', '\\n', np.array(a), '\\n_______________')\r\n",
        "print('y_index:', '\\n', np.array(y_index), '\\n_______________')\r\n",
        "print('y_one_hot:', '\\n', np.array(y_one_hot))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "y: \n",
            " [[['a' 'l']]\n",
            "\n",
            " [['a' 'h']]] \n",
            "_______________\n",
            "y_index: \n",
            " [0 2 0 1] \n",
            "_______________\n",
            "y_one_hot: \n",
            " [[1. 0. 0.]\n",
            " [0. 0. 1.]\n",
            " [1. 0. 0.]\n",
            " [0. 1. 0.]]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "foVT6prNVqfc",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "05e9bdf3-ccd4-461a-8e00-803c194452e1"
      },
      "source": [
        "a"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[[['a', 'l']], [['a', 'h']]]"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 54
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "JM9RJ4TcR1sr",
        "outputId": "a16498cc-3352-4683-92e1-70dcad1cd1f7"
      },
      "source": [
        "a\r\n",
        "np.unique(a)\r\n",
        "\r\n"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array(['a', 'h', 'l'], dtype='<U1')"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 53
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "_mZ6UDwFQS6S",
        "outputId": "28ff6746-c177-4201-f378-a693578343ed"
      },
      "source": [
        "l = {1: 5, 4:4}\r\n",
        "len(l)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "2"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 32
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "jM2BnhMALekZ",
        "outputId": "176e74f0-7bc9-45f8-cbe9-91d5ffeca0d7"
      },
      "source": [
        "\r\n",
        "\r\n",
        "import numpy as np\r\n",
        "\r\n",
        "y = [['cake'], ['tiramisu'], ['cake'], ['sacher']]\r\n",
        "\r\n",
        "#   create index-based encoding dict\r\n",
        "#fill set\r\n",
        "set1 = set()\r\n",
        "[[set1.add(el) for el in a] for a in y]\r\n",
        "\r\n",
        "#create dict\r\n",
        "label_2_int = {list(set1)[i]:i for i in range(len(list(set1)))}\r\n",
        "\r\n",
        "#index-based encoding\r\n",
        "y_index = [[label_2_int[el] for el in a] for a in y]\r\n",
        "\r\n",
        "#one_hot encoding\r\n",
        "def fill_array(array, index):\r\n",
        "  array[index] = 1\r\n",
        "  return array\r\n",
        "\r\n",
        "y_one_hot = [[fill_array(np.zeros(len(set1)), el) for el in a] for a in y_index]\r\n",
        "\r\n",
        "print('y:', '\\n', np.array(y), '\\n_______________')\r\n",
        "print('y_index:', '\\n', np.array(y_index), '\\n_______________')\r\n",
        "print('y_one_hot:', '\\n', np.array(y_one_hot))\r\n",
        "\\\r\n",
        "y: \r\n",
        " [['cake']\r\n",
        " ['tiramisu']\r\n",
        " ['cake']\r\n",
        " ['sacher']] \r\n",
        "_______________\r\n",
        "y_index: \r\n",
        " [[2]\r\n",
        " [1]\r\n",
        " [2]\r\n",
        " [0]] \r\n",
        "_______________\r\n",
        "y_one_hot: \r\n",
        " [[[0. 0. 1.]]\r\n",
        " [[0. 1. 0.]]\r\n",
        " [[0. 0. 1.]]\r\n",
        " [[1. 0. 0.]]]\r\n"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array(['a', 'l', 'b', 'h'], dtype='<U1')"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 22
        }
      ]
    }
  ]
}